package com.bjb.vr.auth.controller;

import com.bjb.vr.auth.bo.UmsUserDetail;
import com.bjb.vr.auth.service.UmsAccountEnhanceService;
import com.bjb.vr.common.ao.UmsAccountAo;
import com.bjb.vr.common.dto.SysTenant;
import com.bjb.vr.common.dto.UmsAccount;
import com.bjb.vr.common.dto.UmsOptAccount;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.SysTenantService;
import com.bjb.vr.common.service.UmsAccountService;
import com.bjb.vr.common.service.UmsOptAccountService;
import io.jsonwebtoken.lang.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 权限管理列表
 *
 * @description:
 * @author: HuJingBo
 * @time: 2022/5/18 13:21
 */
@Slf4j
@RestController
@RequestMapping("/api/auth/v1")
public class AccessController {

    @Resource
    private UmsAccountEnhanceService umsAccountEnhanceService;

    @Resource
    private UmsAccountService accountService;

    @Resource
    private UmsOptAccountService optAccountService;

    @Resource
    private SysTenantService tenantService;

    /**
     * 查询当前登录用户的权限
     *
     * @return
     */
    @PostMapping("/queryAuthorities")
    public CommonResult queryAuthorities() {
        UmsUserDetail userDetails = umsAccountEnhanceService.queryAuthorities();
        return CommonResult.Ok().setResult(userDetails);
    }

    /**
     * 新增租户--教师端
     *
     * @return
     */
    @PostMapping("/addManagerTenant")
    public CommonResult addManagerTenant(@RequestBody UmsAccountAo umsAccountAo) {
        Assert.notNull(umsAccountAo.getTenantName(), "租户名不能为空");
        Assert.notNull(umsAccountAo.getUserName(), "用户名不能为空");
        Assert.notNull(umsAccountAo.getPassword(), "用户密码不能为空");
        Assert.notNull(umsAccountAo.getNickName(), "用户昵称不能为空");
        Assert.notNull(umsAccountAo.getCategory(), "客户类别不能为空");
        SysTenant sysTenant = tenantService.queryTenantByTenantName(umsAccountAo.getTenantName());
        if (Objects.nonNull(sysTenant)) {
            return CommonResult.Fail("租户名已存在");
        }
        UmsAccount account = accountService.getAccountByUsername(umsAccountAo.getUserName());
        if (Objects.nonNull(account)) {
            return CommonResult.Fail("用户名已存在");
        }
        Boolean result = accountService.addManagerTenant(umsAccountAo);
        if (!result) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok("新增成功");
    }

    /**
     * 新增租户--运营端
     *
     * @return
     */
    @PostMapping("/addOperationTenant")
    public CommonResult addOperationTenant(@RequestBody UmsAccountAo umsAccountAo) {
        Assert.notNull(umsAccountAo.getUserName(), "用户名不能为空");
        Assert.notNull(umsAccountAo.getPassword(), "用户密码不能为空");
        Assert.notNull(umsAccountAo.getNickName(), "用户昵称不能为空");
        UmsOptAccount optAccount = optAccountService.getAccountByUsername(umsAccountAo.getUserName());
        if (Objects.nonNull(optAccount)) {
            return CommonResult.Fail("用户名已存在");
        }
        Boolean result = optAccountService.addOperationTenant(umsAccountAo);
        if (!result) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok("新增成功");
    }
}
